package sg4c.frontend.command;

import java.io.IOException;
import sg4c.frontend.GraphicsContext;
import sg4c.protocol.DataInputProtocol;

public class DrawPolygon implements GraphicsCommand {

    private final int[] xPoints;
    private final int[] yPoints;
    
    public DrawPolygon(int[] x, int[] y) {
        assert (x.length == y.length);
        xPoints = x;
        yPoints = y;
    }

    public void execute(GraphicsContext context) {
        if (!context.noFill()) {
            context.loadFillColor();
            context.getGraphics().fillPolygon(xPoints, yPoints, xPoints.length);
        }
        if (!context.noStroke()) {
            context.loadStrokeColor();
            context.getGraphics().drawPolygon(xPoints, yPoints, xPoints.length);
        }
        
    }
    public static final Factory factory = new Factory() {

        public GraphicsCommand parse(DataInputProtocol in) throws IOException {
            final int count = in.readInt();
            final int[] x = new int[count];
            final int[] y = new int[count];
            for (int i = 0; i < count; ++i) {
                x[i] = in.readInt();
                y[i] = in.readInt();
            }
            return new DrawPolygon(x, y);
        }
    };
}
